package model

import (
	"gin/util"
	"github.com/jinzhu/gorm"
	"strconv"
)

type User struct {
	gorm.Model
	Name      string `gorm:"type:varchar(20);not null"`
	Telephone string `gorm:"type:varchar(20);not null;unique"`
	Password  string `gorm:"type:varchar(255);not null"`
}

func IsTelephoneExist(db *gorm.DB, telephone string) bool {
	var user = User{}
	db.Where("telephone=?", telephone).First(&user)
	if user.ID != 0 {
		return true
	}
	return false
}

type UserInfo struct {
	Uid           int    `gorm:"column:uid"`
	Openid        string `gorm:"column:openid"`
	Nick          string `gorm:"column:nick"`
	AvatarUrl     string `gorm:"column:avatarUrl"`
	Country       string `gorm:"column:country"`
	City          string `gorm:"column:city"`
	Province      string `gorm:"column:province"`
	Gender        int    `gorm:"column:gender"`
	LastLoginIp   string `gorm:"column:last_login_ip"`
	LastLoginTime string `gorm:"column:last_login_time"`
	LoginTimes    int    `gorm:"column:login_times"`
	Token         string `gorm:"column:token"`
	CreateTime    string `gorm:"column:create_time"`
	UpdateTime    string `gorm:"column:update_time"`
	Level         int    `gorm:"column:level"`
	Score         int    `gorm:"column:score"`
	AgentId       int    `gorm:"column:agent_id"`
}

type ScoreStruct struct {
	Id      int    `gorm:"column:id"`
	DateAdd string `gorm:"column:dateAdd"`
	Uid     int    `gorm:"column:uid"`
	Score   int    `gorm:"column:score"`
	Type    int    `gorm:"column:type"`
}

func OpenidGetUser(db *gorm.DB, openid string) UserInfo {
	var user = UserInfo{}
	db.Debug().Table("cat_meal_user").Where("openid=?", openid).First(&user)
	return user
}

func UpdateLastLogin(db *gorm.DB, user UserInfo, token string) int {
	db.Debug().Table("cat_meal_user").Where("uid=?", user.Uid).Updates(UserInfo{
		LoginTimes:    user.LoginTimes + 1,
		LastLoginIp:   util.GetLocalIP(),
		LastLoginTime: util.DateFormat(0),
		Token:         token,
	})
	return 1
}

type Score struct {
	Id      int    `gorm:"column:id"`
	Uid     int    `gorm:"column:uid"`
	Score   int    `gorm:"column:score"`
	DateAdd string `gorm:"column:dateAdd"`
	Type    int    `gorm:"column:type"`
}

func ChangeScore(db *gorm.DB, user UserInfo, score int, ScoreType int) error {
	var MyInfo UserInfo
	db.Debug().Table("cat_meal_user").Where("uid=?", user.Uid).First(&MyInfo)

	if err := db.Debug().Table("cat_meal_user").Where("uid=?", user.Uid).Updates(UserInfo{
		Score: MyInfo.Score + score,
	}).Error; err != nil {
		util.LogError("用户UID:" + strconv.Itoa(user.Uid) + "增加用户积分失败")
		db.Rollback()
	}
	scoreInfo := Score{
		Uid:     user.Uid,
		Score:   score,
		Type:    ScoreType,
		DateAdd: util.DateFormat(0),
	}
	if err := db.Debug().Table("cat_meal_user_score").Create(&scoreInfo).Error; err != nil {
		util.LogError("用户UID:" + strconv.Itoa(user.Uid) + "增加用户积分记录失败")
		db.Rollback()
	}
	if ScoreType == 1 {
		scoreInfo := Score{
			Uid:     user.AgentId,
			Score:   score,
			Type:    ScoreType,
			DateAdd: util.DateFormat(0),
		}
		if err := db.Debug().Table("cat_meal_user_score").Create(&scoreInfo).Error; err != nil {
			util.LogError("推荐人用户UID:" + strconv.Itoa(scoreInfo.Uid) + "增加用户积分失败")
			db.Rollback()
		}
		var AgentInfo UserInfo
		db.Debug().Table("cat_meal_user").Where("uid=?", user.AgentId).First(&AgentInfo)

		if err := db.Debug().Table("cat_meal_user").Where("uid=?", user.AgentId).Updates(UserInfo{
			Score: AgentInfo.Score + score,
		}).Error; err != nil {
			util.LogError("推荐人用户UID:" + strconv.Itoa(scoreInfo.Uid) + "增加用户积分记录失败")
			db.Rollback()
		}
	}
	return nil
}

type LevelInfo struct {
	Id            int     `gorm:"column:id"`
	Name          string  `gorm:"column:name"`
	Rebate        float64 `gorm:"column:rebate"`
	UpgradeAmount float64 `gorm:"column:upgradeAmount"`
}
type InsertAddressInfo struct {
	Id         int    `gorm:"column:id"`
	LinkMan    string `gorm:"column:linkMan"`
	Address    string `gorm:"column:address"`
	Mobile     string `gorm:"column:mobile"`
	IsDefault  string `gorm:"column:isDefault"`
	Latitude   string `gorm:"column:latitude"`
	Longitude  string `gorm:"column:longitude"`
	ProvinceId string `gorm:"column:provinceId"`
	CityId     string `gorm:"column:cityId"`
	DistrictId string `gorm:"column:districtId"`
	Uid        int    `gorm:"column:uid"`
}
type AddressInfo struct {
	InsertAddressInfo
	ProvinceStr string
	CityStr     string
	AreaStr     string
}
